import Vue from 'vue';
import { Toast } from 'vant';

const BASE_URL = process.env.API_ROOT
console.log("process.env.API_ROOT==>", process.env)
const RequestUtil = {
  uploadUrl: process.env.UPLOAD_URL,

  post: function (url, params, success) {

    if (arguments.length === 0) {
      return;
    }
    if (arguments.length === 2) {
      if (typeof arguments[1] === 'function') {
        params = new Object();
        success = arguments[1];
      }
    }
    if (params === undefined || params === null) {
      params = new Object();
    }


    Vue.$http.interceptors.push((request, next) => {
      // if(url !== 'user/login') {
      //   request.headers.set('token', window.sessionStorage.getItem("token"));
      // }
      /// request.headers.set('version', '1.0.0');
      next((response) => {
        return response
      })
    });

    Vue.http.post(BASE_URL + url, JSON.stringify(params)).then(
      (response) => {
        if (response.body.status === '0000') {
          success(response.body);
        } else {
          Toast(response.body.message);
        }
      },
      (response) => {
      }
    );
  },
  trace: function (obj) {
    if (process.env.NODE_ENV != 'production') {
      console.log(JSON.stringify(obj, null, 4));
    }
  }
}

export default RequestUtil;



// WEBPACK FOOTER //
// ./src/api/RequestUtil.js